package com.mapapp.flutter_mapapp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.blankj.utilcode.util.GsonUtils;
import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.bean.DetailTree;
import com.mapapp.flutter_mapapp.persenter.RegionSelectionPresenter;
import com.mapapp.flutter_mapapp.util.ScreenUtil;
import com.mapapp.flutter_mapapp.util.ToastUtils;
import com.mapapp.flutter_mapapp.view.RegionSelectionViewInterface;
import com.mapapp.flutter_mapapp.widget.TitleBar;
import com.unnamed.b.atv.model.TreeNode;
import com.unnamed.b.atv.view.AndroidTreeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MergeHouseholdRegionActivity extends ImmersiveBaseActivity implements RegionSelectionViewInterface {

    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.tree_container)
    ScrollView treeContainer;
    @BindView(R.id.divider)
    View divider;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;

    private TreeNode root;
    private AndroidTreeView tView;
    private RegionSelectionPresenter presenter;
    private TreeNode currentNode;

    private ArrayList<String> regionIds;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merge_household_region);
        ButterKnife.bind(this);

        init();

        presenter.getTreeList("-1");
    }


    private void init() {
        regionIds = getIntent().getStringArrayListExtra("regionIds");
        presenter = new RegionSelectionPresenter(this, this, lifecycleSubject);
        root = TreeNode.root();

        tView = new AndroidTreeView(this, root);
        treeContainer.setMinimumWidth(ScreenUtil.getScreenWidth(mContext));
        treeContainer.addView(tView.getView());
        tView.setDefaultAnimation(false);
        tView.setDefaultContainerStyle(R.style.TreeNodeStyleCustom);
        tView.setUseAutoToggle(false);
        tView.setDefaultNodeClickListener(new TreeNode.TreeNodeClickListener() {
            @Override
            public void onClick(TreeNode node, Object value) {
                currentNode = node;
                DetailTree tree = (DetailTree) value;
                titleBar.getTitleBarCenter().setText(tree.getRegionName());
                if (!tree.isLeaf() && node.getChildren().size() == 0) {
                    presenter.getTreeList(tree.getRegionId());
                } else {
                    if (tree.isLeaf()) {


                    } else {
                        tView.toggleNode(currentNode);
                        ImageView icon = currentNode.getViewHolder().getView().findViewById(R.id.expand);
                        icon.setSelected(currentNode.isExpanded());
                    }
                }
            }
        });


        tvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailTree nodeValue = (DetailTree) currentNode.getValue();
                if (currentNode != null && nodeValue.isLeaf()) {
                    if (regionIds != null && regionIds.contains(nodeValue.getRegionId())) {
                        ToastUtils.showMessage(mContext, "该户号已经添加过");
                        return;
                    }
                    Intent intent = new Intent();
                    intent.putExtra("detailTree", GsonUtils.toJson(currentNode.getValue()));
                    setResult(1, intent);
                    finish();
                }
            }
        });

        titleBar.getIvBack().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

    }

    @Override
    public void showTreeList(List<DetailTree> detailTreeList) {
        if (detailTreeList != null && !detailTreeList.isEmpty()) {
            if (detailTreeList.get(0).getLevel() == 1) {
                for (DetailTree detailTree : detailTreeList) {
                    TreeNode treeNode = new TreeNode(detailTree).setViewHolder(new MyHolder(mContext));
                    root.addChild(treeNode);
                }
                tView.expandNode(root);
                titleBar.getTitleBarCenter().setText(detailTreeList.get(0).getRegionName());
            } else {
                for (DetailTree detailTree : detailTreeList) {
                    TreeNode treeNode = new TreeNode(detailTree).setViewHolder(new MyHolder(mContext));
                    treeNode.setSelected(false);
                    currentNode.addChild(treeNode);
                }
                tView.expandNode(currentNode);
                ImageView icon = currentNode.getViewHolder().getView().findViewById(R.id.expand);
                icon.setSelected(true);
            }
        }
    }

    @Override
    public void showUpdateNode(List<DetailTree> detailTreeList) {

    }

    class MyHolder extends TreeNode.BaseNodeViewHolder<DetailTree> {

        MyHolder(Context context) {
            super(context);
        }

        @Override
        public View createNodeView(TreeNode node, DetailTree value) {
            final LayoutInflater inflater = LayoutInflater.from(context);
            final View view = inflater.inflate(R.layout.item_parent_node, null, false);
            TextView tvValue = view.findViewById(R.id.tv_title);
            tvValue.setText(value.getRegionName());

            ImageView expand = view.findViewById(R.id.expand);
            if (value.isLeaf()) {
                expand.setVisibility(View.INVISIBLE);
            } else {
                expand.setVisibility(View.VISIBLE);
            }

            return view;
        }

    }
}
